<?php
namespace wow\model\dbcon;

/**
 * Auth connection lib.
 * @author peec
 *
 */
class WorldDB extends \gnomephp\doctrine\DoctrineConnection implements \Iterator{
	
	/**
	 * World db.
	 * @var string
	 */
	const WORLD = 'world';
	/**
	 * Character db
	 * @var string
	 */
	const CHAR = 'char';
	
	
	/**
	 * Array list of entity managers.
	 */
	static public $ems = array();
	
	/**
	 * Do not load entity manager several times on single page.
	 * 
	 * @var boolean
	 */
	static public $loaded=false;	
	
	public $position = 1;
	
	
	private $worldEM;
	private $charEM;
	
	private $soap = array('enable_soap' => false);
	
	public function __construct($id = null){
		WorldDB::load($id);
		$this->worldEM = WorldDB::$ems[$id][WorldDB::WORLD];
		$this->charEM = WorldDB::$ems[$id][WorldDB::CHAR];
		
		$dbs = \gnomephp\Configuration::get('worlddbs', $id);
		if (isset($dbs['soap']))$this->soap = $dbs['soap'];
	}
	/**
	 * Entity manager for characters database.
	 * @return Doctrine\ORM\EntityManager
	 */
	public function getCharEM(){
		return $this->charEM;
	}
	
	/**
	 * Entity manager for world database.
	 * @return Doctrine\ORM\EntityManager
	 */
	public function getWorldEM(){
		return $this->worldEM;
	}
	
	/**
	 * Returns soap connection if soap connection is enabled.
	 * If not enabled it returns null.
	 * @return wow\model\Soap
	 */
	public function createSoapConnection(){
		if ($this->soap['enable_soap']){
			return new \wow\model\Soap(
				$this->soap['admin_user'],
				$this->soap['admin_password'],
				$this->soap['host'],
				$this->soap['port']
			);
		}
		return null;
	}
	
	/**
	 * Returns the world and character db for the specific realm id.
	 * @param int $id ID in the realmlist.
	 * @return wow\dbcon\WorldDB
	 */
	static public function getEmFor($id){
		return new WorldDB($id);
	}
	
	
	
	/**
	 * Loads up all world connections.
	 * 
	 * @param int $id Default is null, if id is given - the world connection specific id will be loaded.
	 */
	static public function load($id=null){
		parent::load();
		
		if ($id){
			if (!isset(self::$ems[$id])){
				
				$dbs = \gnomephp\Configuration::get('worlddbs', $id);
				self::$ems[$id][WorldDB::WORLD] = self::getNewEntityManager($dbs[WorldDB::WORLD]);
				self::$ems[$id][WorldDB::CHAR] = self::getNewEntityManager($dbs[WorldDB::CHAR]);

			}
		}else if (!self::$loaded){
			
			$sets = \gnomephp\Configuration::get('worlddbs');
			foreach($sets as $id => $set){
				self::$ems[$id][WorldDB::WORLD] = self::getNewEntityManager($set[WorldDB::WORLD]);
				self::$ems[$id][WorldDB::CHAR] = self::getNewEntityManager($set[WorldDB::CHAR]);
			}
			self::$loaded = true;
		}
		
	}

 


	/*
	 *  Iterator interface methods.
	 */
	
	
    public function rewind() {
    	$ks = array_keys(self::$ems);
        $this->position = $ks[0];
    }

    public function current() {
        return new WorldDB($this->position);
    }

    public function key() {
        return $this->position;
    }

    public function next() {
        ++WorldDB::$position;
    }

    public function valid() {
        return isset(WorldDB::$ems[$this->position]);
    }

}